const url = 'http://14.18.41.247:8060/valid/api/queryStatus';
var imei = getDeviceIdentity();


theads.start(function () { 
    setTimeout(function () {
        checkStatus();
    }, 60000);
})


function checkStatus() {
    let r = http.post(url, {
        'imei': imei
    })
    if (r.statusCode == 200) {
        let rs = r.body.json();
        if (rs.code != 0) {
            toastLog(rs.msg)
        } else if (rs.code == 0 && rs.data['type'] == 0) {
            toastLog('卡密已过期，请重新激活!\n到期时间为:' + rs.data['dateTime']);
            engines.stopAll();
            exit();
        }
    }
}


function getDeviceIdentity() {
    let id = null;
    if (device.sdkInt >= 29) {
        let mac = device.getMacAddress()
        if (mac == null || mac == "" || mac == "00:00:00:00:00:00") return null
        return "M-" + xx(mac.replace(/:/, ""))
    }
    try {
        id = android.os.Build.getSerial();
    } catch (e) {
        if (e.javaException instanceof java.lang.SecurityException)
            return null;
    }
    if (id && id.toLowerCase() != "unknown") {
        id = xx(id)
    } else if (device.serial && device.serial.toLowerCase() != "unknown") {
        id = xx(device.serial)
    } else {
        let iii = null;
        let tm = context.getSystemService("phone");
        if (!tm) return null;
        try {
            iii = tm.getImei(0);
        } catch (e) {
            if (e.javaException instanceof java.lang.SecurityException)
                return null;
        }
        if (iii == null || iii == "000000000000000") {
            iii = device.getIMEI();
        }
        if (iii != null) id = "I-" + xx(iii);
        else return null
    }
    return id;
}

function xx(str) {
    var mod = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz"
    var arr = str.split("");
    for (var i in arr) arr[i] = mod[(mod.indexOf(arr[i]) + 10) % mod.length]
    return arr.reverse().join("")
}